/*
 * Niiuzu
 * Copyright (C) 2011  Thierry Nowak
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.niiuzu.client.screen.application;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;
import com.niiuzu.client.util.widget.LoadingBox;
import com.niiuzu.client.util.widget.error.BusinessErrorWidget;
import com.niiuzu.client.util.widget.error.TechnicalErrorWidget;
import com.niiuzu.client.util.widget.popup.PopupModal;

/**
 * Implémentation de la vue de l'application.
 */
public class ApplicationViewImpl extends Composite implements ApplicationView {

	/**
	 * Interface {@link UiBinder} adaptée à la vue {@link ApplicationViewImpl}.
	 */
	interface SuitedUiBinder extends UiBinder<Widget, ApplicationViewImpl> {

	}

	@UiField
	Panel mainPanel;

	private PopupModal loadingPopup;
	private LoadingBox loadingBox;

	@Override
	public void createView() {
		SuitedUiBinder uiBinder = GWT.create(SuitedUiBinder.class);
		initWidget(uiBinder.createAndBindUi(this));

		loadingBox = new LoadingBox();
		loadingPopup = new PopupModal(loadingBox);
	}

	@Override
	public String toString() {
		return this.getClass().toString();
	}

	@Override
	public void replaceWidgetInMainPanelWith(IsWidget widget) {
		mainPanel.clear();
		mainPanel.add(widget);
	}

	@Override
	public void showPopupError(Throwable throwable) {
		TechnicalErrorWidget errorWidget = new TechnicalErrorWidget();
		errorWidget.setError(throwable);

		createAndShowPopup(errorWidget, errorWidget.getCloseButton());
	}

	@Override
	public void showPopupErrorMessage(String errorMessage) {
		BusinessErrorWidget errorWidget = new BusinessErrorWidget();
		errorWidget.setErrorMessage(errorMessage);

		createAndShowPopup(errorWidget, errorWidget.getCloseButton());
	}

	/**
	 * Crée et affiche une nouvelle popup.
	 * 
	 * @param bodyWidget
	 *            {@link Widget} constituant le corps de la popup.
	 * @param closeComponent
	 *            composant destiné à fermer la popup.
	 */
	private void createAndShowPopup(Widget bodyWidget,
			HasClickHandlers closeComponent) {
		PopupModal popup = new PopupModal(bodyWidget);
		popup.bindPopupCloser(closeComponent);
		popup.center();
	}

	@Override
	public void hideLoadingMessage() {
		loadingPopup.hide();
	}

	@Override
	public void showLoadingMessage(String message) {
		loadingBox.setHTML(message);
		loadingPopup.center();
	}
}
